<?


class JCATMVCObjectContainerView extends JCAT_ViewGtk implements JCAT_IViewForm
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	array
	 */
	public function JCATMVCObjectContainerView( JCAT_IViewContainer $aParent, $bControllerOperation )
	{
		$this->JCAT_ViewGtk('JCATMVCObjectContainerView','JCATMVCObjectContainerView','JCATMVCObjectContainerView',$aParent) ;
		$this->AddUICtrl(new TreeJCATMVCObject($bControllerOperation),'Children') ;
	}
	
	
	/**
	 * @see JCAT_ViewGtk::OnInitView()
	 *
	 */
	protected function OnInitView ()
	{
		$aUICtrl = $this->GetUICtrl('TreeJCATMVCObject') ;
		$aCellRender = $aUICtrl->GetCellRender(TreeJCATMVCObject::CLM_GLOBAL_NAME) ;
		$aCellRender->connect_after('edited',array($this,'OnGlobalNameChanged')) ;
	}

	
	/**
	 * 更新子控件清单
	 * 
	 * @access	public
	 * @return	void
	 */
	public function LoadChildrenObject()
	{
		// 得到模型
		$aTreeJCATMVCObject = $this->GetUICtrl('TreeJCATMVCObject') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTreeJCATMVCObject) ;
		$aModel = $aTreeJCATMVCObject->GetGtkModel() ;
		if(!$aModel)
		{
			return ;
		}

		// 清除 已有的
		$aModel->clear() ;
		
		// 交由完成 加载
		$aDoc = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDoc) ;
		$aDoc->LoadChildrenObject($aModel) ;
		
		
		// 显示 全局名称
		$aDocument = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDocument,'JCAT_IDocument') ;
		$arrGlobalNames = $aDocument->Get('GlobalNames') ;
	
		$aIterator = $this->CreateMVCObjectIterator() ;
		$aModel = $aIterator->GetModel() ;
		
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$aItem = $aIterator->Current() ;
			
			$sName = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_NAME) ;
			if( isset($arrGlobalNames[$sName]) )
			{
				$sGlobalName = $arrGlobalNames[$sName] ;
			}
			else 
			{
				$sGlobalName = '' ;
			}
			
			$aModel->set($aItem,TreeJCATMVCObject::CLM_GLOBAL_NAME,$sGlobalName) ;
			
			$aIterator->Next() ;
		}
	}
	
	/**
	 * 响应删除按钮
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBtnDeleteOptionClicked()
	{		
		// 当前选中的 item
		list($aModel,$aItem) = $this->GetSelectedItem() ;
		
		if(!$aItem)
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('请选择要删除的项目。')) ;
			return ;
		}

		$sPath = $aModel->get_string_from_iter($aItem) ;
		if( strstr($sPath,':')!==false )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('只能删除直属的项目。')) ;
			return ;
		}

		$aModel->remove($aItem) ;
		$this->ExchangeData(JCAT_IViewForm::UICTRL_TO_DOC,'TreeJCATMVCObject') ;
	}
	
	/**
	 * 响应 BtnEditIUCtrl 按钮
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnBtnEditIUCtrlClicked()
	{
		list($aModel,$aItem) = $this->GetSelectedItem(true) ;
		if(!$aItem)
		{
			return ;
		}

		$sItemPath = $aModel->get_string_from_iter($aItem) ;
		if( strstr($sItemPath,':')!==false )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('只能编辑直属的项目。')) ;
			return ;
		}
		
		// UI控件的 相对路径
		$sRelativePath = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_PATH) ;
		
		$aParentController = $this->GetParentController() ;
		$aTopController = $aParentController->GetTopController() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTopController) ;
		$sThisPath = $aTopController->GetPath() ;
		
		if( is_file($sThisPath) )
		{
			$sThisPath = dirname($sThisPath) ;
		}
		
		// UI控件的 路径
		$arrArg['path'] = JCAT_Global::TidyPath($sThisPath).$sRelativePath ;
		
		if( is_file($arrArg['path']) )
		{
			$aOpen = new OpenCoderDocument() ;
			$aOpen->MainRun( &$arrArg ) ;
		}
	}
	

	public function GetSelectedItem($bAlert=false)
	{
		$aTreeUICtrlChildren = $this->GetUICtrl('TreeJCATMVCObject') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aTreeUICtrlChildren,'JCAT_UIGtkCtrlTree') ;
		
		$wndTreeUICtrlChildren = $aTreeUICtrlChildren->GetMainWidget() ;
		JCAT_ASSERT::ASSERT_INSTANCE($wndTreeUICtrlChildren,'GtkTreeView') ;
		
		// 当前选中的 item
		$aSelection = $wndTreeUICtrlChildren->get_selection() ;
		list($aModel,$aItem) = $aSelection->get_selected() ;
		
		if(!$aItem)
		{
			if($bAlert)
			{
				JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('请选择要删除的项目。')) ;
			}
			return array($aModel,null) ;
		}
		
		else 
		{
			return array($aModel,$aItem) ;
		}
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	JCAT_GtkTreeItemIterator
	 */
	public function CreateMVCObjectIterator()
	{
		$aUICtrl = $this->GetUICtrl('TreeJCATMVCObject') ;
		return $aUICtrl->CreateItemIterator(null,-1) ;
	}

	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnGlobalNameChanged(GtkCellRendererText $aRender, $sPath, $sNewText)
	{$aRender;$sPath;$sNewText;
		$arrGlobalNames = array() ;
		
		$aIterator = $this->CreateMVCObjectIterator() ;
		$aModel = $aIterator->GetModel() ;
		
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$aItem = $aIterator->Current() ;
			
			$sGlobalName = trim($aModel->get_value($aItem,TreeJCATMVCObject::CLM_GLOBAL_NAME)) ;
			if($sGlobalName)
			{
				$sName = $aModel->get_value($aItem,TreeJCATMVCObject::CLM_NAME) ;
				$arrGlobalNames[$sName] = $sGlobalName ;
			}
			
			$aIterator->Next() ;
		}
		
		// 保存到文档
		$aDocument = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDocument,'JCAT_IDocument') ;
		
		$aDocument->Set('GlobalNames',$arrGlobalNames) ;
	}
}
?>